package com.testmanager.testmananger.shuiyou.strCompare.web;


import com.testmanager.testmananger.shuiyou.strCompare.domain.SyCompareTaskEntity;
import com.testmanager.testmananger.shuiyou.strCompare.domain.SyDbManagerEntity;
import com.testmanager.testmananger.shuiyou.strCompare.domain.SyProjectEntity;
import com.testmanager.testmananger.shuiyou.strCompare.domain.SySqlCompareScriptEntity;
import com.testmanager.testmananger.shuiyou.strCompare.dto.*;
import com.testmanager.testmananger.shuiyou.strCompare.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.Response;
import java.util.List;

@RestController
@RequestMapping("/strCompare")
@Slf4j
public class CompareResource {

    /**
     * 工程项目操作
     * @return
     */
    @Autowired
    SyProjectService syProjectService;

    @PostMapping ("/getproject")
    public ResponseEntity getProject(){
       // log.info("");
        return syProjectService.getSyProject();
    }

    @PostMapping("/saveproject")
    public ResponseEntity saveProject(@RequestBody SyProjectEntity syProjectEntity){
        return syProjectService.saveOrUpdateSyProject(syProjectEntity);
    }

    @PostMapping("/updateproject")
    public ResponseEntity updateProject(@RequestBody SyProjectEntity syProjectEntity){
        return syProjectService.saveOrUpdateSyProject(syProjectEntity);
    }

    /**
     * 数值比较脚本操作
     * @return
     */
@Autowired
    SqlCompareScriptService sqlCompareScriptService;

    @PostMapping("deleteScript/{scriptno}")
    public ResponseEntity deleteScript(@PathVariable("scriptno") String scriptno){
        return  sqlCompareScriptService.deleteScript(scriptno);
    }


    @PostMapping("/getsqlCompareScript")
    public ResponseEntity  getSqlCompareScripts(@RequestBody SqlCompareScriptSearchDto sqlCompareScriptSearchDto){
        return sqlCompareScriptService.search(sqlCompareScriptSearchDto);
    }


    @PostMapping("/savesqlCompareScript")
    public ResponseEntity saveSqlCompareScript(@RequestBody CompareScriptDto sySqlCompareScriptEntity){
        return sqlCompareScriptService.saveOrUpdate(sySqlCompareScriptEntity);
    }

    @PostMapping("/updatesqlCompareScript")
    public ResponseEntity updateSqlCompareScript(@RequestBody CompareScriptDto sySqlCompareScriptEntity){

        return sqlCompareScriptService.saveOrUpdate(sySqlCompareScriptEntity);
    }
    //上传excel
    @PostMapping("uploadsqlCompareScripts")
    public ResponseEntity uploadsqlCompareScripts(@RequestParam(value = "file",required = false) MultipartFile file){
        return  sqlCompareScriptService.saveFromExcel(file);
    }


    /*
    暂不实现
     */
    @PostMapping("/delSqlCompareScript/{id}")
    public ResponseEntity delSqlCompareScript(@PathVariable("id")long id){
        return null;
    }


    /**
     * 数据库维护操作
     */

    @Autowired
    SyDbManagerService dbManagerService;
    @PostMapping("/getdbs")
    public ResponseEntity getDbs(@RequestBody DbManagerSearchDto dbManagerSearchDto){
        return  dbManagerService.search(dbManagerSearchDto);
    }
    @PostMapping("/getalldbs")
    public ResponseEntity getDbs(){
        return  dbManagerService.getAllDbs();
    }

    @PostMapping("/savedb")
    public ResponseEntity saveDb(@RequestBody SyDbManagerEntity dbManagerEntity){
        return dbManagerService.saveOrUpdate(dbManagerEntity);
    }

    @PostMapping("/updatedb")
    public ResponseEntity updateDb(@RequestBody SyDbManagerEntity dbManagerEntity){

        return dbManagerService.saveOrUpdate(dbManagerEntity);
    }

    @PostMapping("/dbverify")
    public ResponseEntity dbverify(@RequestBody SyDbManagerEntity dbManagerEntity){
        return dbManagerService.verifycheck(dbManagerEntity);
    }

    @PostMapping("/getdbByProjectId/{projectId}")
    public ResponseEntity getDbbyProjectid(@PathVariable("projectId") long projectid){
        return dbManagerService.getDbsByProjectId(projectid);
    }


    /**
     * 比对任务管理操作
     */

    @Autowired
    SqlCompareTaskService taskService;

    @PostMapping("/deleteTask/{taskno}")
    public ResponseEntity deletetask(@PathVariable("taskno") String taskno){
        ResponseEntity responseEntity = taskService.deleteTask(taskno);
        return responseEntity;
    }

    @PostMapping("/saveTasks")
    public ResponseEntity  saveBatchTasks(@RequestBody List<SyCompareTaskDto> taskDtos){
        return  taskService.batchTask(taskDtos);
    }

    @PostMapping("/getAllSqlCompareTasks")
    public ResponseEntity getAllSqlCompareTasks(@RequestBody  CompareTasksSearchDto compareTasksSearchDto){

       // log.info("getAllSqlCompareTasks:::::===={}=====",compareTasksSearchDto);

        return taskService.search(compareTasksSearchDto);
    }



    @PostMapping("/saveSqlCompareTask")
    public ResponseEntity saveSqlCompareTask(@RequestBody SyCompareTaskDto compareTaskEntity){
        return taskService.save(compareTaskEntity);
    }

    @PostMapping("updateSqlCompareTask")
    public ResponseEntity updateSqlCompareTask(@RequestBody SyCompareTaskDto taskDto){
        return taskService.update(taskDto);
    }

    @PostMapping("delSqlCompareTask/{id}")
    public ResponseEntity delSqlCompareTask(@PathVariable("id") long id){
        return null;
    }

    /**
     * 执行任务
     */
    @Autowired
    ExceuteTaskService exceuteTaskService;
    @PostMapping("/executeTask/{taskId}")
    public ResponseEntity executeTask(@PathVariable("taskId")long taskid){
        return exceuteTaskService.execute(taskid);
    }



    @PostMapping("/executeTasks")
    public ResponseEntity executeTasks(@RequestBody List<SyCompareTaskEntity> tasks){
        return exceuteTaskService.executeTasks(tasks);
    }


    /**
     *  任务结果查询
     */

    @Autowired
    SyCompareResultService resultService;

    @PostMapping("/getResultsByTask/{taskId}")
    public ResponseEntity getResultsByTask(@PathVariable("taskId")long taskid){
        return  resultService.getResult(taskid);
    }

    @PostMapping("/getDetailResult/{resultId}")
    public ResponseEntity getResultBynearest(@PathVariable("resultId")long resultId){
        return resultService.getDetailResult(resultId);
    }

    @PostMapping("/getAllResults")
    public ResponseEntity getAllResults(){
        return  resultService.getAllResult();
    }



    @Autowired
    ExportExcelService exportExcelService;
    @GetMapping ("/downloadall")
    public void downloadall(HttpServletResponse response) throws Exception {
        exportExcelService.downloadAll(response);
    }

   // http://127.0.0.1:9091/strCompare/downloadall

    @PostMapping ("/downloadResult")
    public void downloadResult(HttpServletResponse response,@RequestBody List<SyCompareTaskEntity> taskEntities) throws Exception {
        log.info("{}",taskEntities);

        //return  ResponseEntity.ok("succe");
        exportExcelService.downloadResult(response
               ,taskEntities);

    }


    @PostMapping("/uploadScript/{projectid}")
    public ResponseEntity  uploadscript(@PathVariable("projectid")long projectid,@RequestBody List<CompareScriptDto>  scriptDtos){
        return  exportExcelService.uploadScript(projectid,scriptDtos);
    }





}
